package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.utils.RequestUtil;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsAppAdMapper;
import com.yuke.cloud.service.cmc.entity.CmsAppAd;
import com.yuke.cloud.service.cmc.service.CmsAppAdService;
import com.yuke.cloud.common.core.support.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by wg on 2019/01/18.
 */
@Service
@Transactional
public class CmsAppAdServiceImpl extends BaseService<CmsAppAd> implements CmsAppAdService {
    @Resource
    private CmsAppAdMapper cmsAppAdMapper;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;


    @Override
    public int save(CmsAppAd record) {
        record.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        return super.save(record);
    }

    @Override
    public int update(CmsAppAd entity) {
        entity.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        int count = super.update(entity);
        if (count > 0) {
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            if (entity.getOldPicUrl() != null && !"".equals(entity.getOldPicUrl())) {
                filePaths.add(entity.getOldPicUrl());
                uploadFile.setFilePaths(filePaths);
                opcFileFeignApi.filesDelete(uploadFile);
            }
        }
        return count;
    }

    @Override
    public void deleteByIds(String ids) {
        List<CmsAppAd> cmsAppAds = cmsAppAdMapper.selectByIds(ids);
        super.deleteByIds(ids);
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (CmsAppAd cmsAppAd : cmsAppAds) {
            if (cmsAppAd.getOldPicUrl() != null && !"".equals(cmsAppAd.getOldPicUrl()))
                filePaths.add(cmsAppAd.getOldPicUrl());
        }
        uploadFile.setFilePaths(filePaths);
        opcFileFeignApi.filesDelete(uploadFile);
    }

    @Override
    public int updateList(List<CmsAppAd> cmsAppAds) {
        int count = 0;
        for (CmsAppAd cmsAppAd : cmsAppAds) {
            count += this.update(cmsAppAd);
        }
        return count;
    }

    @Override
    public List<CmsAppAd> getListDetailByCondition(CmsAppAd cmsAppAd) {
        return cmsAppAdMapper.getListDetailByCondition(cmsAppAd);
    }
}
